package com.hksj.Intelligentmanagement.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.Intelligentmanagement.common.Constants;
import com.hksj.Intelligentmanagement.dto.HqEnvironmentHistoryDataDTO;
import com.hksj.Intelligentmanagement.dto.RoomEnvSyncQuery;
import com.hksj.Intelligentmanagement.entity.Orinfotable;
import com.hksj.Intelligentmanagement.mapper.HqEnvironmentHistoryDataMapper;
import com.hksj.Intelligentmanagement.utils.RedisCache;
import com.hksj.Intelligentmanagement.vo.HqEnvironmentHistoryDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class HqEnvironmentHistoryDataService {

    @Autowired
    private OrinfotableService orinfotableService;

    @Autowired
    private HqEnvironmentHistoryDataMapper hqEnvironmentHistoryDataMapper;

    @Autowired
    private ThreadPoolTaskExecutor asyncServiceExecutor;

    @Autowired
    private RedisCache<Integer> intRedisCache;

    public IPage<HqEnvironmentHistoryDataVo> selectHqEnvironmentHistoryData(HqEnvironmentHistoryDataDTO dto) {
        Page<HqEnvironmentHistoryDataVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        List<HqEnvironmentHistoryDataVo> list =hqEnvironmentHistoryDataMapper.selectHqEnvironmentHistoryData(page,dto);
        page.setRecords(list);
        return page;
    }

    public List<Map<String, Object>> queryTestForHQ(){
        List<Map<String, Object>> resultList = hqEnvironmentHistoryDataMapper.queryTestForHQ();
        return resultList;
    }

    public IPage<Orinfotable> pageRoomEnv(RoomEnvSyncQuery query){
        Page<Orinfotable> page = new Page<>(query.getPageNum(), query.getPageSize());
        List<Orinfotable> list =  hqEnvironmentHistoryDataMapper.pageRoomEnv(query,query.getDisAblePage() ? null : page);
        page.setRecords(list);
        return page;
    }

    public void toSyncData(RoomEnvSyncQuery query){
        asyncServiceExecutor.execute(() -> {
            syncData(query);
        });
    }

    public void syncData(RoomEnvSyncQuery query) {
        IPage<Orinfotable> page = pageRoomEnv(query);
        if (!ObjectUtils.isEmpty(page.getRecords())) {
            try{
                orinfotableService.saveOrUpdateBatch(page.getRecords());
                System.out.println("总页数====>" + page.getPages() + "; 当前页====>" + page.getCurrent() + "; " + DateUtil.formatDateTime(new Date()));
            }catch (Exception e){
                System.err.println("err; 总页数====>" + page.getPages() + "; 当前页====>" + page.getCurrent() + "; " + DateUtil.formatDateTime(new Date()));
            }
            if (page.getCurrent() < page.getPages()){
                query.setPageNum(query.getPageNum() + 1);
                syncData(query);
            }
        }
    }

    public void autoSyncData() {
        Object status = intRedisCache.getCacheObject(Constants.ENABLE_AUTO_SYNC_KEY);
        if (!Objects.equals(1,status)){
            return;
        }

        asyncServiceExecutor.execute(() -> {
            RoomEnvSyncQuery roomEnvSyncQuery = new RoomEnvSyncQuery();
            roomEnvSyncQuery.setPageSize(200);
            Orinfotable one = orinfotableService.lambdaQuery()
                    .orderByDesc(Orinfotable::getId)
                    .last("limit 1")
                    .one();
            if (!ObjectUtils.isEmpty(one)) {
                roomEnvSyncQuery.setStartId(one.getId());
                syncData(roomEnvSyncQuery);
            }
        });
    }

    public void enableAutoSync(Integer status) {
        intRedisCache.setCacheObject(Constants.ENABLE_AUTO_SYNC_KEY,status);
    }
}
